package com.item.mofan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.item.mofan.common.Result;
import com.item.mofan.dto.DishDto;
import com.item.mofan.dto.SetmealDto;
import com.item.mofan.entity.*;
import com.item.mofan.service.CategoryService;
import com.item.mofan.service.DishService;
import com.item.mofan.service.SetmealDishService;
import com.item.mofan.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除旧缓存数据
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success("添加套餐成功");
    }
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return Result.success("套餐更新成功");
    }
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){

        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");//对象拷贝，records除外

        List<Setmeal> setmealList=setmealPage.getRecords();//获取Setmeal的List数据

        List<SetmealDto> setmealDtoList=setmealList.stream().map((item)->{
            SetmealDto setmealDto =new SetmealDto();

            BeanUtils.copyProperties(item,setmealDto);//把item数据拷贝给setmealDto对象

            //根据id查找出菜品名称
            Long id=item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category!=null){
                setmealDto.setCategoryName(category.getName());//名称赋值给setmealDto对象
            }
            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);

        return Result.success(setmealDtoPage);
    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("套餐删除成功");
    }

    /**
     * 该分类下的套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#setmeal.getCategoryId()")
    public Result<List<SetmealDto>> list(Setmeal setmeal){
        log.info(setmeal.toString());
        LambdaQueryWrapper<Setmeal>  queryWrapper=new LambdaQueryWrapper<>();
        //展示该分类的套餐
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,1);//展示售卖的套餐
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmealList=setmealService.list(queryWrapper);

        List<SetmealDto> setmealDtoList=setmealList.stream().map((item)->{
            SetmealDto setmealDto =new SetmealDto();

            BeanUtils.copyProperties(item,setmealDto);//把item数据拷贝给setmealDtoDto对象

            LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(SetmealDish::getSetmealId,setmealDto.getId());
            List<SetmealDish> setmealDishes=setmealDishService.list(queryWrapper1);

            setmealDto.setSetmealDishes(setmealDishes);

            return setmealDto;
        }).collect(Collectors.toList());

        return Result.success(setmealDtoList);
    }
    /**
     * 获取套餐详情
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public Result<List<SetmealDish>> getDish(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        return Result.success(setmealDishList);
    }
    @GetMapping("/{id}")
    public Result<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto= setmealService.getByIdWithDish(id);
        return Result.success(setmealDto);
    }
    @PostMapping("/status/1")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> up(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmealList=setmealService.list(queryWrapper);
        setmealList.stream().map((item)->{
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmealList);
        return Result.success("套餐上架成功");
    }

    @PostMapping("/status/0")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> down(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmealList=setmealService.list(queryWrapper);
        setmealList.stream().map((item)->{
            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmealList);
        return Result.success("套餐下架成功");
    }

}
